$(document).ready(function(){
	var date = new Date();
	//设置每行第四个格子的边框
	$('.rows').each(function(){
		$(this).children().eq(3).css({'border-right':'solid 1px black'});
	});
	var cnt=0;
	var start,end;
	function print (e)
	{
		if(e == 0){
				$('.score').find('h1').html('失败！');
		}
		else{
			$('.score').find('h1').html('恭喜<span style="color:red">'+ e + '</span>秒!');
			$('.record').append('<h3>time:'+end+'</h3>');
		}

		var marginTop = $('.score').height()/2;
		var marginLeft = $('.score').width()/2;
		console.log(marginLeft);
		$('#modalShade').fadeIn().addClass('show');
		$('.score').css({
			'margin-top':-marginTop,
			'margin-left':-marginLeft
		});

	}
	$('.count h1').text('开始！');
	$('.arow').on('mousedown','.black',function(){
		cnt++;
		$('.count h1').text(cnt+'格!');
		if(cnt == 1)
			start = new Date();

		$('.arow').animate(
			{top : 1},100,function(){
				var i = Math.floor(Math.random()*4);
				console.log(i);
				$('.rows:last').find('.black').removeClass('black');
				$('.rows:last').children().eq(i).addClass('black');
				$('.rows:first').before($('.rows:last'));
				$(".arow").css({'top':'-159px'});
			});
		
		if(cnt == 30){
			end = (new Date() - start)/1000;
			print(end);
		}
		return false;
	});
	$('.arow').on('mousedown','.block:not(".black")',function(){
		$(this).animate(
			{backgroundColor:'red'},100,function(){
			print(0);
		});
		return false;

	});
	function shadeFadeOut(){
		cnt = 0;
		$('#modalShade').removeClass('show').fadeOut();
		$('.block').not('.black').animate(
			{backgroundColor:'white'},300,function(){;
				$(this).css("background-color","");
			});
		console.log('use!');
		$('.count h1').text('开始！');
	};

	$('#modalShade').click(shadeFadeOut);

	var triggers = {J:0,K:1,L:2,º:3};//按键值对应数值. 'º' 其实就是';'这个东西通过String.fromCharCode转换过来的
	$('body').keydown(function(event){
		var key = String.fromCharCode(event.keyCode);//把按键值转化成ASCII字符
		if(!$('#modalShade').hasClass('show'))
		{
			if(key in triggers){
				$('.rows:last').children().eq(triggers[key]).trigger('mousedown');//把键盘事件转化为鼠标事件
			}
		}
		else{
			shadeFadeOut();
		}
	});
	

});